import { createRouter, createWebHashHistory } from 'vue-router';
import { baseRoutes, notFoundAndNoPower } from './route';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { Session } from '/@/utils/storage';

const router = createRouter({
	history: createWebHashHistory(),
	routes: [...baseRoutes, ...notFoundAndNoPower],
});

// @ts-ignore
router.beforeEach(async (to: any, from: any, next: any) => {
	NProgress.configure({ showSpinner: false });
	if (to.name) NProgress.start();
	const token = Session.getToken();
	if (to.meta.isAuth !== undefined && !to.meta.isAuth) {
		next();
		NProgress.done();
	} else {
		if (token && to.path === '/login') {
			next('/home');
			NProgress.done();
		} else {
			next();
		}
	}
});

// 路由加载后
router.afterEach(() => {
	NProgress.done();
});
export default router;
